package de.dailab.graphworld.bean.utils;

import de.dailab.graphworld.ontology.ContractNetProtocol;
import de.dailab.graphworld.ontology.Edge;
import de.dailab.graphworld.ontology.Job;
import de.dailab.graphworld.ontology.Node;
import de.dailab.graphworld.ontology.Proposal;
import de.dailab.graphworld.ontology.Task;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Kseniya
 */
public abstract class BeanUtils {

    public static Collection<Task> findTasksByNode(Collection<Task> tasks, Node node) {
        Collection<Task> ret = new LinkedList<>();
        for (Task task : tasks) {
            if (task.goldNode.equals(node)) {
                ret.add(task);
            }
        }
        return ret;
    }

    public static Job findBestJob(Collection<Job> jobs) {
        if (jobs.isEmpty()) {
            throw new RuntimeException("Can't select a job, no jobs supplied!");
        }
        Iterator<Job> itr = jobs.iterator();
        Job best = itr.next();
        while (itr.hasNext()) {
            Job job = itr.next();
            if (best.getTask().goldNode.getGold() < job.getTask().goldNode.getGold()) {
                best = job;
            }
        }
        return best;
    }
    
    public static <J extends Job> void removeJobsByTask(Collection<J> jobs, Task task) {
        List<J> jobsFound = findJobsByTask(jobs, task);
        jobs.removeAll(jobsFound);
    }

    public static <J extends Job> List<J> findJobsByTask(Collection<J> jobs, Task task) {
        List<J> jobsFound = new LinkedList<>();
        for (J job : jobs) {
            if (task.equals(job.getTask())) {
                jobsFound.add(job);
            }
        }
        return jobsFound;
    }
    
    public static Proposal createRefusal(Task task) {
        List<Edge> emptyPath = Collections.emptyList();
        return new Proposal(task, -1, emptyPath);
    }
    
    public static ContractNetProtocol selectProposalType(int capacity) {
        return capacity >= 0 ? ContractNetProtocol.PROPOSE : ContractNetProtocol.REFUSE;
    }
}
